﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kooboo.IoC;
using Kooboo.Commerce;
using Kooboo.Commerce.Contexts.Accounts;
using Kooboo.Commerce.Contexts.Customers;



namespace Kooboo.Web.Areas.Commerce
{
    public class RoleProvider : System.Web.Security.RoleProvider
    {

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var host = new ServiceHost();
            foreach (var i in usernames)
            {
                if (roleNames.Contains(Roles.Administrator))
                {
                    var admin = ObjectContainer.CreateInstance<IAdministrator>();
                    admin.UserName = i;

                    host.Post(admin);
                }

                if (roleNames.Contains(Roles.Customer))
                {
                    var customer = ObjectContainer.CreateInstance<ICustomer>();
                    customer.UserName = i;

                    host.Post(customer,Kooboo.Commerce.Configuration.DefaultPassword);
                }
            }
        }

        public override string ApplicationName
        {
            get;
            set;
        }

        public override void CreateRole(string roleName)
        {
            //
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            return false;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var host = new ServiceHost();
            if (roleName == Roles.Administrator)
            {
                
                return host.QueryAdministrators()
                    .Where(i=>i.UserName.Contains(usernameToMatch))
                    .Select(i => i.UserName)
                    .ToArray();
            }
            else if (roleName == Roles.Customer)
            {
                return host.QueryCustomers()
                    .Where(i => i.UserName.Contains(usernameToMatch))
                    .Select(i => i.UserName)
                    .ToArray();
            }
            else
            {
                return new string[]{};
            }
        }

        public override string[] GetAllRoles()
        {
            return new string[] { Roles.Administrator,Roles.Customer };
        }

        public override string[] GetRolesForUser(string username)
        {
            var host = new ServiceHost();

            List<string> roles = new List<string>();
            

            if (host.QueryAdministrators().Any(i => i.UserName == username))
            {
                roles.Add(Roles.Administrator);
            }

            if (host.QueryCustomers().Any(i => i.UserName == username))
            {
                roles.Add(Roles.Customer);
            }

            return roles.ToArray();

        }

        public override string[] GetUsersInRole(string roleName)
        {
            var host = new ServiceHost();
            if (roleName == Roles.Administrator)
            {
                
                return host
                    .QueryAdministrators()
                    .Select(i => i.UserName)
                    .ToArray();
            }
            else if (roleName == Roles.Customer)
            {
                return host
                    .QueryCustomers()
                    .Select(i => i.UserName)
                    .ToArray();
            }
            else
            {
                return new string[] { };
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var host = new ServiceHost();
            if (roleName == Roles.Administrator)
            {
                return host
                    .QueryAdministrators()
                    .Any(i => i.UserName == username);
            }
            else if (roleName == Roles.Customer)
            {
                return host
                    .QueryCustomers()
                    .Any(i => i.UserName == username);
            }
            else
            {
                return false;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            var host = new ServiceHost();
            foreach (var i in usernames)
            {
                if (roleNames.Contains(Roles.Administrator))
                {
                    var admin = ObjectContainer.CreateInstance<IAdministrator>();
                    admin.UserName = i;

                    
                    host.Delete(admin);
                }

                if (roleNames.Contains(Roles.Customer))
                {
                    var customer = ObjectContainer.CreateInstance<ICustomer>();
                    customer.UserName = i;

                    host.Delete(customer);
                }
            }
        }

        public override bool RoleExists(string roleName)
        {
            return roleName == Roles.Administrator || roleName == Roles.Customer;
        }


        class Roles
        {
            public readonly static string Administrator = "Administrator";
            public readonly static string Customer = "Customer";
        }
    }
}
